// UI类
class UI {
    constructor(game) {
        this.game = game;
    }

    // 更新UI
    updateUI() {
        this.updateLives();
        this.updateRound();
        this.updateAttackPower();
        this.updateTargets();
        this.updateBossHealth();
    }

    // 更新生命值显示
    updateLives() {
        const livesElement = document.getElementById('lives');
        livesElement.textContent = this.game.lives;
    }

    // 更新回合数显示
    updateRound() {
        const roundElement = document.getElementById('current-round');
        roundElement.textContent = `${this.game.currentRound}/${this.game.maxRounds}`;
    }

    // 更新攻击力显示
    updateAttackPower() {
        // 检查是否已存在攻击力显示元素
        let attackPowerElement = document.getElementById('attack-power');
        
        if (!attackPowerElement) {
            // 创建攻击力显示元素
            attackPowerElement = document.createElement('div');
            attackPowerElement.id = 'attack-power';
            document.getElementById('game-container').appendChild(attackPowerElement);
        }
        
        attackPowerElement.textContent = `攻击力: ${this.game.attackPower}`;
    }

    // 更新怪物击败数量显示
    updateMonsterCount() {
        // 这个方法现在被 updateTargets() 代替，但保留以兼容旧代码
    }

    // 新增：更新目标显示
    updateTargets() {
        // 获取目标显示区域
        let targetContainer = document.getElementById('target');
        if (!targetContainer) {
            // 如果不存在，创建
            targetContainer = document.createElement('div');
            targetContainer.id = 'target';
            document.getElementById('level-info').appendChild(targetContainer);
        }

        // 清空目标容器
        targetContainer.innerHTML = '';
        
        // 添加目标标题
        const titleElement = document.createElement('div');
        titleElement.textContent = '目标:';
        targetContainer.appendChild(titleElement);
        
        // 添加每个目标的显示
        for (const target of this.game.targets) {
            const targetElement = document.createElement('div');
            targetElement.className = 'target-item';
            targetElement.textContent = target.description;
            
            // 检查目标是否完成
            const currentValue = target.currentValue || 0;
            if (currentValue >= target.count) {
                targetElement.classList.add('target-completed');
            }
            
            targetContainer.appendChild(targetElement);
        }
        
        // 保持单独的怪物计数(兼容性代码)
        let monsterCountElement = document.getElementById('monster-count');
        if (!monsterCountElement) {
            monsterCountElement = document.createElement('div');
            monsterCountElement.id = 'monster-count';
            document.getElementById('game-container').appendChild(monsterCountElement);
        }
        
        const monsterTarget = this.game.targets.find(t => t.type === "monsters");
        if (monsterTarget) {
            monsterCountElement.textContent = monsterTarget.description;
        }
    }

    // 更新Boss生命值显示
    updateBossHealth() {
        // 查找所有Boss元素并更新生命值显示
        const bossElements = document.querySelectorAll('.boss');
        bossElements.forEach(bossElement => {
            const cell = bossElement.closest('.cell');
            if (cell) {
                // 查找或创建生命值显示元素
                let healthElement = cell.querySelector('.boss-health');
                if (!healthElement) {
                    healthElement = document.createElement('div');
                    healthElement.className = 'boss-health';
                    cell.appendChild(healthElement);
                }
                
                // 更新生命值
                healthElement.textContent = this.game.bossHealth;
            }
        });
    }

    // 高亮显示格子
    highlightCell(row, col) {
        const cell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
        if (cell) {
            cell.classList.add('selected');
        }
    }

    // 绘制连接线
    drawConnectionLine(fromRow, fromCol, toRow, toCol) {
        // 暂时只高亮目标格子
        const toCell = document.querySelector(`.cell[data-row="${toRow}"][data-col="${toCol}"]`);
        if (toCell) {
            toCell.classList.add('selected');
        }
    }

    // 清除高亮和连接线
    clearHighlights() {
        // 清除高亮效果
        const cells = document.querySelectorAll('.cell.selected');
        cells.forEach(cell => cell.classList.remove('selected'));
    }

    // 显示游戏结束界面
    showGameOver(isWin) {
        // 创建游戏结束界面
        const gameOverDiv = document.createElement('div');
        gameOverDiv.className = 'game-over';
        
        // 设置文本
        const messageDiv = document.createElement('div');
        messageDiv.textContent = isWin ? '恭喜你赢了！' : '游戏结束';
        gameOverDiv.appendChild(messageDiv);
        
        // 添加重新开始按钮
        const restartButton = document.createElement('button');
        restartButton.textContent = '重新开始';
        restartButton.addEventListener('click', () => {
            gameOverDiv.remove();
            this.game.restart();
        });
        gameOverDiv.appendChild(restartButton);
        
        // 添加到文档
        document.getElementById('game-container').appendChild(gameOverDiv);
    }
}